/**
  ******************************************************************************
  * @file    Project/STM32F4xx_StdPeriph_Templates/main.c 
  * @author  MCD Application Team	/ 蒋晟松
  * @version V1.8.0
  * @date    04-November-2016
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "key.h"
#include "led.h"
#include "beep.h"
#include "int_key.h"
#include "systick.h"
#include "music.h"
#include "Usart.h"
/** @addtogroup Template_Project
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static __IO uint32_t uwTimingDelay;
RCC_ClocksTypeDef RCC_Clocks;

/* Private function prototypes -----------------------------------------------*/
static void Delay(__IO uint32_t nTime);

// 使用其他文件里面的变量,int_key.c
extern uint8_t status;
extern uint8_t usart_status;

#define on 1
#define off 0
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  led_init();
  key_init();
  BEEP_Init();
  key_int_init();

  usart_init();
  // 注意这个需要在keil的配置里面勾选一下 use microlib
  printf("hello world\r\n");

  Usart_SendByte(USART1, 0x60);

  // printf("%x", Usart_ReceiveByte(USART1));

  // 直接在最开始运行的时候，usart发送一个字符A
  // USART_SendData(USART1, 0x41);
  int time = 0;
  uint8_t res = 0;
  // while (1)
  // {
  //   // res = Usart_ReceiveByte(USART1);
  //   // printf("%x of time %d", res, time);
  //   // 虽然接收过来的是一个8位的数，但似乎也可以直接与字符串进行比较
  //   // if (res == '1')
  //   // {
  //   //   led_loop();
  //   // }
  //   // delay(0x944444);
  //   if (usart_status == '1')
  //   {
  //     led_loop();
  //   }
  //   else if (usart_status == '2')
  //   {
  //     music_fun();
  //   }
  //   time++;
  // }

  //  int status[5] = {0};
  //  int delayBeepTime = 0x444444;
  /* Infinite loop */

  while (1)
  {

    if (status == 1)
    {
      music_fun();
      led_loop();
    }

    //	if(!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0))
    //	{
    //		GPIO_SetBits(GPIOF, GPIO_Pin_8);	// 这个是响
    //		delay(delayBeepTime);
    //		GPIO_ResetBits(GPIOF, GPIO_Pin_8);	// 这个是不响
    //		led_loop();
    //	}

    //	if(key_check(1)){
    //		led_loop();
    //		status[1] = 1;
    //	}
    //	if(key_check(2)){
    //		beep_on();
    //		status[2] = 1;
    //	}
    //	if(key_check(3)){
    //		status[3] = 1;
    //		beep_off();
    //	}
  }
}

/**
  * @brief  Inserts a delay time.
  * @param  nTime: specifies the delay time length, in milliseconds.
  * @retval None
  */
void Delay(__IO uint32_t nTime)
{
  uwTimingDelay = nTime;

  while (uwTimingDelay != 0)
    ;
}

/**
  * @brief  Decrements the TimingDelay variable.
  * @param  None
  * @retval None
  */
void TimingDelay_Decrement(void)
{
  if (uwTimingDelay != 0x00)
  {
    uwTimingDelay--;
  }
}

#ifdef USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
